# Maintainer: Justin Kromlinger <hashworks@archlinux.org>

pkgname=opensearch-alerting-plugin
pkgver=2.6.0.0
pkgrel=1
_opensearchver=2.6.0
_jdkver=11
pkgdesc="OpenSearch Alerting Plugin"
arch=('loong64' 'x86_64')
url="https://docs-beta.opensearch.org/monitoring-plugins/alerting"
license=('Apache')
depends=("opensearch=${_opensearchver}" "opensearch-notifications-plugin")
makedepends=("java-environment=${_jdkver}" 'unzip')
source=(
  "${pkgname}-${pkgver}.tar.gz::https://github.com/opensearch-project/alerting/archive/${pkgver}.tar.gz"
)
sha256sums=('14e5450267364ddb0936ed97bb2d904222945ef43a96403d090152ebfcb08e2b')

build() {
  cd "alerting-${pkgver}"
  export JAVA_HOME="/usr/lib/jvm/java-${_jdkver}-openjdk"
  export PATH="/usr/lib/jvm/java-${_jdkver}-openjdk/bin:$PATH"
  export GRADLE_OPTS="-Dbuild.snapshot=false -Dopensearch.version=${_opensearchver}"
  # alertingBwcCluster and integTest (Reaper) requires JDK 14
  ./gradlew build \
    --exclude-task ":alerting:alertingBwcCluster#fullRestartClusterTask" \
    --exclude-task ":alerting:alertingBwcCluster#mixedClusterTask" \
    --exclude-task ":alerting:alertingBwcCluster#oldVersionClusterTask0" \
    --exclude-task ":alerting:alertingBwcCluster#oldVersionClusterTask1" \
    --exclude-task ":alerting:alertingBwcCluster#rollingUpgradeClusterTask" \
    --exclude-task ":alerting:alertingBwcCluster#twoThirdsUpgradedClusterTask" \
    --exclude-task ":alerting:integTest" \
    --exclude-task ":alerting:jacocoTestReport"
}

package() {
  install -dm755 "${pkgdir}/usr/share/opensearch/plugins/opensearch-alerting"
  cd "${pkgdir}/usr/share/opensearch/plugins/opensearch-alerting"
  unzip "${srcdir}/alerting-${pkgver}/alerting/build/distributions/opensearch-alerting-${pkgver}.zip"

  install -Dm644 "${srcdir}/alerting-${pkgver}/LICENSE.txt" "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE.txt"
}
